<?php

//define cache path

	class cache {

	public $check_cache = false;

	/**

	 * save data to cache

	 *

	 * @param: mixed $name

	 * @param: mixed $value

	 * @param: (int) $timeout

	 */

	

	public function save( $name, $value, $timeout = 0,$sub="") {

		$name=md5($name);

		//start cache file

		$cache_file = "<?PHP\n\n";

		

		//set description to cache file

		$cache_file .= "/*\n	CACHE FILE NAME: ". $name ."\n	GENERATE TIME: ". date('c') ."\n*/\n\n";

		

		//set data to cache file

		$timeout = $timeout + time();

		$cache_file .= "//cache timeout\n" . '$timeout = '. $timeout . ";\n\n";

		

		//set variable to cache

		$cache_file .= "//cache content\n" . '$cache_variable = '. var_export($value, true) .';';

		

		//end cache

		$cache_file .= "\n\n?>";

		

		//return cache to save

		if($sub!=""){

				$path=CACHE_PATH.'/'.trim($sub);

		}else{

				$path=CACHE_PATH;

		}

		if(!is_dir($path)){

					mkdir($path,0775,true);

		}

		

	

		return file_put_contents( $path .'/'. $name .'.php', $cache_file);

		

	}

	

	/**

	 * load cache 

	 *

	 * @return: mixed

	 */

	

	public function load( $name,$sub="") {

		$name=md5($name);

		//chech was cache exists

			if($sub!=""){

				$path=CACHE_PATH.'/'.trim($sub);

			}else{

				$path=CACHE_PATH;

			}

			

			

			

			if( file_exists($path.'/'. $name .'.php')) {

				//load cache

			include $path.'/'. $name .'.php';

			

			//chech was cache no expired

			if($timeout > time()) {



				

			}

			else

			{

			return false;

			}

			

			// return cache

			return $cache_variable;

			

			} else {

			

				// return error

				return false;

			}

	}

	

	/**

	 * check value of check_cache field

	 * 

	 */

	



	/**

	 * delete cache

	 *

	 * @param: mixed $name;

	 */

	 

	public function flush( $name,$sub="") {

		$name=md5($name);

		if($sub!=""){

				$path=CACHE_PATH.'/'.trim($sub);

			}else{

				$path=CACHE_PATH;

			}

		if( file_exists($path.'/'. $name .'.php')) {

			unlink($path.'/'. $name .'.php');

		}

	} 

}